package com.february.todo.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.february.todo.tools.Bean_todo;
import com.february.todo.tools.Bean_date;
import com.february.todo.tools.TimeComparator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DataBaseOperate {
    public SQLiteDatabase db;

    public DataBaseOperate(Context context) {
        this.db = new DataBase(context, "todolist").getWritableDatabase();
    }

    public ArrayList<Bean_date> getAllDate() {
        String sql = "SELECT DISTINCT date,timeEnd,isFinished FROM 'todolist'";
        Cursor cursor = db.rawQuery(sql, null);

        int isFinished;
        int state;
        String date;
        String timeEnd;
        Bean_date bean_date;
        ArrayList<Bean_date> AllDate = new ArrayList<Bean_date>();

        if (cursor != null && cursor.moveToFirst()) {
            do {
                date = cursor.getString(0);
                timeEnd = cursor.getString(1);
                isFinished = cursor.getInt(2);

                if (isFinished == 0) {
                    if (new TimeComparator(date, timeEnd).isEarlier) {
                        state = 1;
                    } else {
                        state = 0;
                    }
                } else {
                    state = 2;
                }
                bean_date = new Bean_date(date, state);
                AllDate.add(bean_date);
            } while (cursor.moveToNext());
        }
        assert cursor != null;
        cursor.close();
        return AllDate;
    }

    public Bean_todo gatSelectedTodo(int id) {
        String sql = "SELECT  * FROM 'todolist' WHERE id=" + String.valueOf(id);
        Cursor cursor = db.rawQuery(sql, null);
        Bean_todo bean_todo = new Bean_todo();
        cursor.moveToFirst();
        bean_todo.setId(cursor.getInt(0));
        bean_todo.setDate(cursor.getString(1));
        bean_todo.setTitle(cursor.getString(2));
        bean_todo.setDetail(cursor.getString(3));
        bean_todo.setTimeStart(cursor.getString(4));
        bean_todo.setTimeEnd(cursor.getString(5));
        bean_todo.setPhotoPath(cursor.getString(6));

        if (cursor.getInt(7) == 0) {
            if (new TimeComparator(bean_todo.getDate(), bean_todo.getTimeEnd()).isEarlier) {
                bean_todo.setState(1);
            } else {
                bean_todo.setState(0);
            }
        } else {
            bean_todo.setState(2);
        }
        cursor.close();
        return bean_todo;
    }

    public ArrayList<Bean_todo> getAllTodo() {
        String sql = "SELECT  * FROM 'todolist' ORDER BY date DESC, timeStart ASC";
        Cursor cursor = db.rawQuery(sql, null);

        int id;
        int state;
        int isFinished;
        String timeStart;
        String timeEnd;
        String date;
        String title;
        String detail;
        String photoPath;
        Bean_todo todo;

        ArrayList<Bean_todo> AllTodo = new ArrayList<Bean_todo>();

        if (cursor != null && cursor.moveToFirst()) {
            do {
                id = cursor.getInt(0);
                date = cursor.getString(1);
                title = cursor.getString(2);
                detail = cursor.getString(3);
                timeStart = cursor.getString(4);
                timeEnd = cursor.getString(5);
                photoPath = cursor.getString(6);
                isFinished = cursor.getInt(7);

                if (isFinished == 0) {
                    if (new TimeComparator(date, timeEnd).isEarlier) {
                        state = 1;
                    } else {
                        state = 0;
                    }
                } else {
                    state = 2;
                }
                todo = new Bean_todo(id, date, String.valueOf(timeStart), String.valueOf(timeEnd), title, detail, photoPath, state);
                AllTodo.add(todo);
            } while (cursor.moveToNext());
        }
        assert cursor != null;
        cursor.close();
        return AllTodo;
    }

    public int countTodo(String date) {
        String sql = "SELECT  COUNT(*) FROM 'todolist' WHERE date=" + "'" + date + "'";
        Cursor cursor = db.rawQuery(sql, null);
        cursor.moveToFirst();
        int count = cursor.getInt(0);
        cursor.close();
        return count;
    }

    public void insertTodo(Bean_todo bean_todo) {
        ContentValues cv = new ContentValues();
        cv.put("date", bean_todo.getDate());
        cv.put("title", bean_todo.getTitle());
        cv.put("detail", bean_todo.getDetail());
        cv.put("timeStart", bean_todo.getTimeStart());
        cv.put("timeEnd", bean_todo.getTimeEnd());
        cv.put("photoPath", bean_todo.getPhotoPath());
        cv.put("isFinished", 0);
        long i = db.insert("todolist", null, cv);
    }

    public void updateTodo(Bean_todo bean_todo) {
        ContentValues cv = new ContentValues();
        cv.put("date", bean_todo.getDate());
        cv.put("title", bean_todo.getTitle());
        cv.put("detail", bean_todo.getDetail());
        cv.put("timeStart", bean_todo.getTimeStart());
        cv.put("timeEnd", bean_todo.getTimeEnd());
        cv.put("photoPath", bean_todo.getPhotoPath());
        if (bean_todo.getState() == 2) {
            cv.put("isFinished", 1);
        } else {
            cv.put("isFinished", 0);
        }
        db.update("todolist", cv, "id=?", new String[]{String.valueOf(bean_todo.getId())});
    }

    public void setFinished(int isFinished, int id) {
        ContentValues cv = new ContentValues();
        cv.put("isFinished", isFinished);
        db.update("todolist", cv, "id=?", new String[]{String.valueOf(id)});
    }

    public void deleteTodo(int id) {
        db.delete("todolist", "id=?", new String[]{String.valueOf(id)});
    }
}
